/**
 * 
 */
package au.edu.swin.videoshop.posapp.ui.sections;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import javax.ejb.EJB;
import javax.faces.component.UIData;
import javax.faces.component.UIViewRoot;
import javax.faces.context.FacesContext;

import au.edu.swin.videoshop.bean.Customer;
import au.edu.swin.videoshop.bean.Rental;
import au.edu.swin.videoshop.bean.Reservation;
import au.edu.swin.videoshop.service.CustomerService;
import au.edu.swin.videoshop.service.RentalService;
import au.edu.swin.videoshop.service.ReservationService;

/**
 * @author julie
 *
 */
public class CustomerSection {
	private Customer mSelectedCustomer;
	private String mMode;
	private List mCustomers;
	
	private List mReservations;
	private List mRentals;
	
	@EJB
	private CustomerService mCustomerService;
	
	@EJB
	private ReservationService mReservationService;

	@EJB
	private RentalService mRentalService;
	
	public CustomerSection() {
	}

	
	/**
	 * @return the customers
	 */
	public List getCustomers() {
		if (mCustomers == null) {
			mCustomers = mCustomerService.findAllCustomers();
		}
		return mCustomers;
	}

	/**
	 * @return the selectedCustomer
	 */
	public Customer getSelectedCustomer() {
		return mSelectedCustomer;
	}

	/**
	 * @param selectedCustomer the selectedCustomer to set
	 */
	public void setSelectedCustomer(Customer selectedCustomer) {
		mSelectedCustomer = selectedCustomer;
	}
	
	protected void sortRentalsWithComparator(Comparator comparator) {
		Collections.sort(mRentals, comparator);
	}
	
	protected void sortReservationsWithComparator(Comparator comparator) {
		Collections.sort(mReservations, comparator);
	}

	public String sortRentalsByTitle() {
		sortRentalsWithComparator(new Rental.TitleNameComparator());
		return null;
	}

	public String sortRentalsByMediumType() {
		sortRentalsWithComparator(new Rental.MediumTypeComparator());
		return null;
	}
	
	public String sortRentalsByStore() {
		sortRentalsWithComparator(new Rental.StoreNameComparator());
		return null;
	}
	
	public String sortRentalsByDueDate() {
		sortRentalsWithComparator(new Rental.DueDateComparator());
		return null;
	}
	
	public String sortRentalsByReturnDate() {
		sortRentalsWithComparator(new Rental.ReturnDateComparator());
		return null;
	}
	
	public String sortRentalsByStatus() {
		sortRentalsWithComparator(new Rental.StatusComparator());
		return null;
	}
	
	public String sortReservationsByTitle() {
		sortReservationsWithComparator(new Reservation.TitleNameComparator());
		return null;
	}

	public String sortReservationsByMediumType() {
		sortReservationsWithComparator(new Reservation.MediumTypeComparator());
		return null;
	}
	
	public String sortReservationsByStore() {
		sortReservationsWithComparator(new Reservation.StoreNameComparator());
		return null;
	}
	
	public String sortReservationsByExpiryDate() {
		sortReservationsWithComparator(new Reservation.ExpiryDateComparator());
		return null;
	}

		
	public List getReservations() {
		return mReservations;
	}
	
	public List getRentals() {
		return mRentals;
	}

	public String add() {
		mSelectedCustomer = new Customer();
		mMode = "add";
		return "add";
	}
	
	public String edit() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		UIViewRoot root = facesContext.getViewRoot();
		UIData table = (UIData)root.findComponent("customerForm").findComponent("customerTable");
		mSelectedCustomer = (Customer) table.getRowData();
		
		mMode = "edit";
		return "edit";
	}
	
	public String view() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		UIViewRoot root = facesContext.getViewRoot();
		UIData table = (UIData)root.findComponent("customerForm").findComponent("customerTable");
		mSelectedCustomer = (Customer) table.getRowData();
		if (mSelectedCustomer != null) {
			mReservations = mReservationService.findReservationsByCustomerId(mSelectedCustomer.getCustomerId().longValue());mRentals = mRentalService.findRentalsByCustomerId(mSelectedCustomer.getCustomerId().longValue());
			mRentals = mRentalService.findRentalsByCustomerId(mSelectedCustomer.getCustomerId().longValue());
			sortRentalsByStatus();
		}
		
		return "view";
	}
	
	public String save() {
		if (mSelectedCustomer != null) {
			if (mMode.equals("add")) {
				mCustomerService.addCustomer(mSelectedCustomer);
			}
			else if (mMode.equals("edit")) {
				mCustomerService.updateCustomer(mSelectedCustomer);
			}
			
			mCustomers = null;
		}
		
		return "browse";
	}
	
	public String delete() {
		FacesContext facesContext = FacesContext.getCurrentInstance();
		UIViewRoot root = facesContext.getViewRoot();
		UIData table = (UIData)root.findComponent("customerForm").findComponent("customerTable");
		Customer customer = (Customer) table.getRowData();
		mCustomerService.markDeleteCustomer(customer.getCustomerId());
		
		mCustomers = mCustomerService.findAllCustomers();
		
		return null;
	}
}
